package com.helloandroid.services

import androidx.core.content.edit
import com.helloandroid.MyApplication
import com.helloandroid.tools.GUtils
import com.helloandroid.tools.IntentValue
import com.helloandroid.tools.MyTimeUtils


enum class ChiFanTime {
    ZAO,
    ZHONG,
    XIAWU,
    WAN,
    XIAOYE,
    UNKNOW,
}

class LocalChiFanInfo(

        var bFlagChiFan: Boolean = true, //  true表示玩家打开了 餐补提醒

        // 上次提醒的时间
        var zaoRemindTimeMs: Long = 0L,
        var zhongRemindTimeMs: Long = 0L,
        var xiaWuRemindTimeMs: Long = 0L,
        var wanRemindTimeMs: Long = 0L,
        var xiaoYeRemindTimeMs: Long = 0L,

        // 上次打卡的时间
        var zaoTimeMs: Long = 0L,
        var zhongTimeMs: Long = 0L,
        var xiaWuTimeMs: Long = 0L,
        var wanTimeMs: Long = 0L,
        var xiaoYeTimeMs: Long = 0L,
) {
    companion object {
        const val PrefChiFan = "s_pref_chifan"

        // 吃饭相关的打卡时间
        const val KeyCfZaoTimeMs = "key_cf_zao_time"
        const val KeyCfZhongTimeMs = "key_cf_zhong_time"
        const val KeyCfXiaWuTimeMs = "key_cf_xiawu_time"
        const val KeyCfWanTimeMs = "key_cf_wan_time"
        const val KeyCfXiaoYeTimeMs = "key_cf_xiaoye_time"

        // remind
        const val KeyCfZaoTimeMs_Remind = "key_cf_zao_time_remind"
        const val KeyCfZhongTimeMs_Remind = "key_cf_zhong_time_remind"
        const val KeyCfXiaWuTimeMs_Remind = "key_cf_xiawu_time_remind"
        const val KeyCfWanTimeMs_Remind = "key_cf_wan_time_remind"
        const val KeyCfXiaoYeTimeMs_Remind = "key_cf_xiaoye_time_remind"

        // flag
        const val KeyFlagChiFan = "key_flag_chifan"


        // 09：00 +- 5
        // 早
        const val Zao_Mid = 9 * GUtils.OneHourMs
        const val Zao_Begin = Zao_Mid - 5 * GUtils.OneMinutes
        const val Zao_End = Zao_Mid + 5 * GUtils.OneMinutes

        // 中
        const val Zhong_Mid = 12 * GUtils.OneHourMs
        const val Zhong_Begin = Zhong_Mid - 5 * GUtils.OneMinutes
        const val Zhong_End = Zhong_Mid + 5 * GUtils.OneMinutes

        // 下午茶
        const val XiaWu_Mid = 15 * GUtils.OneHourMs
        const val XiaWu_Begin = XiaWu_Mid - 5 * GUtils.OneMinutes
        const val XiaWu_End = XiaWu_Mid + 5 * GUtils.OneMinutes

        // 晚餐
        const val Wan_Mid = 18 * GUtils.OneHourMs
        const val Wan_Begin = Wan_Mid - 5 * GUtils.OneMinutes
        const val Wan_End = Wan_Mid + 5 * GUtils.OneMinutes

        // 宵夜
        const val XiaoYe_Mid = 22 * GUtils.OneHourMs
        const val XiaoYe_Begin = XiaoYe_Mid - 5 * GUtils.OneMinutes
        const val XiaoYe_End = XiaoYe_Mid + 5 * GUtils.OneMinutes
    }

    fun init() {
        val pref = MyApplication.getPerference(PrefChiFan)

        bFlagChiFan = pref.getBoolean(KeyFlagChiFan, true)

        zaoTimeMs = pref.getLong(KeyCfZaoTimeMs, 0L)
        zhongTimeMs = pref.getLong(KeyCfZhongTimeMs, 0L)
        xiaWuTimeMs = pref.getLong(KeyCfXiaWuTimeMs, 0L)
        wanTimeMs = pref.getLong(KeyCfWanTimeMs, 0L)
        xiaoYeTimeMs = pref.getLong(KeyCfXiaoYeTimeMs, 0L)

        // 提醒的时间
        zaoRemindTimeMs = pref.getLong(KeyCfZaoTimeMs_Remind, 0L)
        zhongRemindTimeMs = pref.getLong(KeyCfZhongTimeMs_Remind, 0L)
        xiaWuRemindTimeMs = pref.getLong(KeyCfXiaWuTimeMs_Remind, 0L)
        wanRemindTimeMs = pref.getLong(KeyCfWanTimeMs_Remind, 0L)
        xiaoYeRemindTimeMs = pref.getLong(KeyCfXiaoYeTimeMs_Remind, 0L)
    }

    fun clearData() {
        MyApplication.getPerference(PrefChiFan).edit(commit = true) {
            clear()
        }
    }

    private fun canTip(curDayTime: String, remindTimeMs: Long, dakaTimeMs: Long): Boolean {
        // 1. 已经打卡,则不能提醒
        // 2. 已经提示就不提示了
        val remindDayTime = MyTimeUtils.dayTimeString(remindTimeMs)
        return if (curDayTime == remindDayTime) {
            // 已经提醒过了
            false
        } else {
            // 判断是否已经打卡了
            curDayTime != MyTimeUtils.dayTimeString(dakaTimeMs)
        }
    }

    fun check(service: ServerService, curTimeMs: Long): Boolean {
        // 条件: 1. 玩家是否开启了吃饭提醒
        //       2. 是否已经提醒过了
        //       3. 是否需要提醒? 已打卡就不提醒
        // 早 中  下午茶  晚  宵夜
        val curDayTime = MyTimeUtils.dayTimeString(curTimeMs)
        val pair = chifanCanRemind(curTimeMs, curDayTime)
        if (pair.first) {
            val pref = MyApplication.getPerference(PrefChiFan)
            var title: String
            var desc: String
            val key: String = when (pair.second) {
                ChiFanTime.ZAO -> {
                    title = "早餐时间到啦"
                    desc = "按时吃饭,还能巨额补贴,赶紧来领取吧!"
                    zaoRemindTimeMs = curTimeMs
                    KeyCfZaoTimeMs_Remind
                }
                ChiFanTime.ZHONG -> {
                    title = " 午餐时间到啦"
                    desc = "按时吃饭,还能巨额补贴,赶紧来领取吧!"
                    zhongRemindTimeMs = curTimeMs
                    KeyCfZhongTimeMs_Remind
                }
                ChiFanTime.XIAWU -> {
                    title = "下午茶时间到啦"
                    desc = "按时吃饭,还能巨额补贴,赶紧来领取吧!"
                    xiaWuRemindTimeMs = curTimeMs
                    KeyCfXiaWuTimeMs_Remind
                }
                ChiFanTime.WAN -> {
                    title = "晚餐时间到啦"
                    desc = "按时吃饭,还能巨额补贴,赶紧来领取吧!"
                    wanRemindTimeMs = curTimeMs
                    KeyCfWanTimeMs_Remind
                }
                ChiFanTime.XIAOYE -> {
                    title = "宵夜时间到啦"
                    desc = "按时吃饭,还能巨额补贴,赶紧来领取吧!"
                    xiaoYeRemindTimeMs = curTimeMs
                    KeyCfXiaoYeTimeMs_Remind
                }
                ChiFanTime.UNKNOW -> {
                    title = "unknow"
                    desc = "unknow"
                    "unknow"
                }
            }
            service.sendRemindNotify(title, desc, IntentValue.Int_ChiFan)
            pref.edit(commit = true) {
                putLong(key, curTimeMs)
            }
            return true
        }
        return false
    }

    private fun chifanCanRemind(curTimeMs: Long, curDayTime: String): Pair<Boolean, ChiFanTime> {
        var cfTime = ChiFanTime.UNKNOW
        val flag = when (MyTimeUtils.getTodayOffsetMs(curTimeMs)) {
            in Zao_Begin..Zao_End -> {
                cfTime = ChiFanTime.ZAO
                canTip(curDayTime, zaoRemindTimeMs, zaoTimeMs)
            }

            // 中
            in Zhong_Begin..Zhong_End -> {
                cfTime = ChiFanTime.ZHONG
                canTip(curDayTime, zhongRemindTimeMs, zhongTimeMs)
            }

            // 下午茶
            in XiaWu_Begin..XiaWu_End -> {
                cfTime = ChiFanTime.XIAWU
                canTip(curDayTime, xiaWuRemindTimeMs, xiaWuTimeMs)
            }

            // 晚
            in Wan_Begin..Wan_End -> {
                cfTime = ChiFanTime.WAN
                canTip(curDayTime, wanRemindTimeMs, wanTimeMs)
            }

            // 宵夜
            in XiaoYe_Begin..XiaoYe_End -> {
                cfTime = ChiFanTime.XIAOYE
                canTip(curDayTime, xiaoYeRemindTimeMs, xiaoYeTimeMs)
            }

            else -> false
        }
        return flag to cfTime
    }

    fun saveChifan(cfTime:ChiFanTime,time:Long){
        when(cfTime){
            ChiFanTime.ZAO->{
                MyApplication.getPerference(PrefChiFan).edit(commit = true){
                    putLong(KeyCfZaoTimeMs,time)
                }
                zaoTimeMs = time
            }

            ChiFanTime.ZHONG->{
                MyApplication.getPerference(PrefChiFan).edit(commit = true){
                    putLong(KeyCfZhongTimeMs,time)
                }
                zhongTimeMs = time
            }

            ChiFanTime.XIAWU->{
                MyApplication.getPerference(PrefChiFan).edit(commit = true){
                    putLong(KeyCfXiaWuTimeMs,time)
                }
                xiaWuTimeMs = time
            }

            ChiFanTime.WAN->{
                MyApplication.getPerference(PrefChiFan).edit(commit = true){
                    putLong(KeyCfWanTimeMs,time)
                }
                wanTimeMs = time
            }

            ChiFanTime.XIAOYE->{
                MyApplication.getPerference(PrefChiFan).edit(commit = true){
                    putLong(KeyCfXiaoYeTimeMs,time)
                }
                xiaoYeTimeMs = time
            }
            else -> Unit
        }
    }

    fun updateFlag(newFlag:Boolean){
        if (newFlag != bFlagChiFan){
            bFlagChiFan = newFlag
            MyApplication.getPerference(PrefChiFan).edit(commit = true){
                putBoolean(KeyFlagChiFan,newFlag)
            }
        }
    }
}